package com.example.usermanager.config;

import com.example.usermanager.tools.AppConfig;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//自定义一个登录拦截器
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //根据是否登录返回true或false，是否处于登录状态根据session判断
        HttpSession session = request.getSession(false);
        if(session != null && session.getAttribute(AppConfig.USERINFO_SESSION_KEY) != null){
            return true;
        }
        //未登录
        return false;
    }
}
